package dao

import (
	"RedWork/model"
	"RedWork/util"
	"fmt"
)

type Phone_Sort struct {
	Search    string
	Brand     string
	PowerMin  string
	PowerMax  string
	ScreenMin string
	ScreenMax string
	IPV6      string
	PriceMax  string
	PriceMin  string
	Code      string
	StoreID   string
	IfTwo     bool
}

func QueryPhone(sort Phone_Sort) (data []model.PhoneInfo) {
	code := 1
	strSql := "select * from phone_info where create_time is not null"
	if sort.Search != "" {
		tempsql := fmt.Sprintf(" and (good_name like '%%%s%%')", sort.Search)
		strSql = strSql + tempsql
	}
	if sort.Brand != "" {
		strSql = strSql + " and (brand='" + sort.Brand + "')"
		code = 1
	}
	if sort.ScreenMin != "" {
		if code == 1 {
			strSql = strSql + " and (screen_size>" + sort.ScreenMin + "&&" + "screen_size<" + sort.ScreenMax + ")"
		} else {
			code = 1
			strSql = strSql + " (screen_size>" + sort.ScreenMin + "&&" + "screen_size<" + sort.ScreenMax + ")"
		}
	}
	if sort.PowerMin != "" {
		if code == 1 {
			strSql = strSql + " and (charging_power>" + sort.PowerMin + "&&" + "charging_power<" + sort.PowerMax + ")"
		} else {
			code = 1
			strSql = strSql + " (charging_power>" + sort.PowerMin + "&&" + "charging_power<" + sort.PowerMax + ")"
		}
	}
	if sort.StoreID != "" {
		strSql = strSql + " and (store_id =" + sort.StoreID + ")"
	}

	if sort.IfTwo {
		strSql = strSql + " and (price>" + sort.PriceMin + "&& price<" + sort.PriceMax + ")"
	}
	if sort.Code == "1" { //按销量排序
		strSql = strSql + " order by turnover desc"
	} else if sort.Code == "2" {
		strSql = strSql + " order by comment_num desc"
	} else if sort.Code == "3" {
		strSql = strSql + " order by create_time desc"
	} else if sort.Code == "4" {
		strSql = strSql + " order by price asc"
	} else if sort.Code == "5" {
		strSql = strSql + " order by price desc"
	} else if sort.Code == "0" {
		strSql = strSql + " and (price>" + sort.PriceMin + " and price<" + sort.PriceMax + ")"
	}

	fmt.Println(strSql)
	rows, err := DB.Query(strSql)
	defer rows.Close()
	if err != nil {
		if ok, _, _ := util.ErrHandle(err); ok {
			fmt.Println("mysql err")
		}
	}
	phones := make([]model.PhoneInfo, 0)
	fmt.Println("rows=", rows)
	if rows != nil {
		fmt.Println("Enter?")
		for rows.Next() {
			var phone_data model.PhoneInfo
			err := rows.Scan(
				&phone_data.GoodID,
				&phone_data.GoodName,
				&phone_data.GoodImage,
				&phone_data.Brand,
				&phone_data.ScreenSize,
				&phone_data.ChargingPower,
				&phone_data.IPV6,
				&phone_data.Turnover,
				&phone_data.CommentNum,
				&phone_data.CreateTime,
				&phone_data.Price,
				&phone_data.StoreID,
			)
			fmt.Println(phone_data)
			if err != nil {
				if ok, _, _ := util.ErrHandle(err); ok {
					fmt.Println("mysql err")
				}
				break
			}
			phones = append(phones, phone_data)
			//fmt.Println(goods)
		}
	}

	if len(phones) == 0 {
		return nil
	}
	//bytes, _ := json.Marshal(phones)
	//fmt.Println(string(bytes))
	return phones
}

type PhoneAllParm struct {
	UniqueNum int     `json:"unique_num"`
	GoodID    int     `json:"good_id"`
	Color     string  `json:"color"`
	Parm      string  `json:"parm"`
	Price     float64 `json:"price"`
	Image     string  `json:"image"`
}

func QueryPhoneAllParm(goodId string) (all []PhoneAllParm) {
	sqlStr := "select * from unique_id where good_idfk = ?"
	rows, err := DB.Query(sqlStr, goodId)
	if err != nil {
		fmt.Println("err")
		return
	}
	defer rows.Close()
	for rows.Next() {
		var temp PhoneAllParm
		err = rows.Scan(
			&temp.UniqueNum,
			&temp.GoodID,
			&temp.Color,
			&temp.Parm,
			&temp.Price,
			&temp.Image,
		)
		if err != nil {
			fmt.Println(err)
			return nil
		}
		all = append(all, temp)
	}
	return
}
